package com.xysq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.etsy.android.grid.StaggeredGridView;
import com.rockcent.action.CallbackListener;
import com.rockcent.model.BClientCustomerBO;
import com.rockcent.model.CustomerCouponBO;
import com.rockcent.model.constant.RealNameAuthType;
import com.xysq.adapter.WantBuyItemAdapter;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;
import com.xysq.widget.LoadData;
import com.xysq.widget.ScrollListener;

import java.util.List;

import butterknife.InjectView;

public class CustomerCouponActivity extends BaseActivity {
    private SwipeRefreshLayout swipeRefreshLayout;
    private StaggeredGridView staggeredGridView;
    private WantBuyItemAdapter itemAdapter;
    @InjectView(R.id.txt_null_data)
    TextView nullDataTxt;
    @InjectView(R.id.txt_connect_or_public)
    TextView connectOrPublicTxt;
    ScrollListener scrollListener;
    private View footerView;
    String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_coupon);
        getExtra();
        initView();
    }

    private void getExtra() {
        title = getIntent().getStringExtra(OtherTopicsActivity.TITLE);
    }

    public void onBack(View view) {
        onBackPressed();
    }

    //获取用户身份认证状态
    public void toPublish(View view) {
        if (TextUtils.isEmpty(UserInfoKeeper.readPhoneNumber(this))) {
            startActivity(new Intent(this, LoginActivity.class));
        }else {
            appAction.getBClientCustomer(UserInfoKeeper.readPhoneNumber(this), new CallbackListener<BClientCustomerBO>() {
                @Override
                public void onSuccess(BClientCustomerBO data) {
                    if (data != null) {
                        if (data.getIsRealnameAuth() == RealNameAuthType.UNAUTH) {//未实名认证，跳到实名认证页面
                            startActivity(new Intent(CustomerCouponActivity.this, IdentifyActivity.class));
                        } else if (data.getIsRealnameAuth() == RealNameAuthType.FAIL) {
                            Intent intent = new Intent(CustomerCouponActivity.this, IdentifyActivity.class);
                            intent.putExtra(IdentifyActivity.FAIL, IdentifyActivity.FAIL);
                            startActivity(intent);
                        } else if (data.getIsRealnameAuth() == RealNameAuthType.AUTHING) {
                            ToastUtil.showShort(CustomerCouponActivity.this, getResources().getString(R.string.toast_authing));
                        } else if (data.getIsRealnameAuth() == RealNameAuthType.AUTH) {
                            Intent intent = new Intent(CustomerCouponActivity.this, PublishGoodsActivity.class);
                            startActivity(intent);
                        }
                    }else {
                        ToastUtil.showShort(CustomerCouponActivity.this, "获取认证信息失败");
                    }
                }

                @Override
                public void onFailure(String errorCode, String message) {

                }
            });
        }
    }

    private void initView() {
        footerView = LayoutInflater.from(this).inflate(R.layout.footer_load_more, null);
        staggeredGridView = (StaggeredGridView) findViewById(R.id.staggered_grid_view);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                reloadData();
            }
        });

        itemAdapter = new WantBuyItemAdapter(this, appAction);
        staggeredGridView.setAdapter(itemAdapter);
        staggeredGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CustomerCouponBO couponBO = (CustomerCouponBO) itemAdapter.getItem(position);
                Intent intent = new Intent(getApplicationContext(), TipsActivity.class);
                intent.putExtra(TipsActivity.COUPONMODEL_ID, couponBO.getId());
                intent.putExtra(TipsActivity.CUSTOMER_ID, couponBO.getCustomerId());
                startActivity(intent);
            }
        });
        scrollListener = new ScrollListener(staggeredGridView, true, new LoadData() {
            @Override
            public void loadMyData() {
                requestData();
            }
        });
        staggeredGridView.setOnScrollListener(scrollListener);
        connectOrPublicTxt.setText(title);
    }

    private void reloadData() {
        scrollListener.setCurrentPage(1);
        scrollListener.setLoadFinish(true);
        itemAdapter.clearItems();
        requestData();
    }

    private void requestData() {
        appAction.listCustomerCoupons(UserInfoKeeper.readCustomerId(this), scrollListener.getCurrentPage(), new CallbackListener<List<CustomerCouponBO>>() {
            @Override
            public void onSuccess(List<CustomerCouponBO> data) {
                if (scrollListener.getCurrentPage() == 1) {
                    if (data.size() == 0) {
                        nullDataTxt.setVisibility(View.VISIBLE);
                    } else {
                        nullDataTxt.setVisibility(View.GONE);
                        itemAdapter.setItems(data);
                    }
                } else {
                    nullDataTxt.setVisibility(View.GONE);
                    if (data.size() > 0) {
                        scrollListener.setLoadFinish(true);
                        itemAdapter.addItems(data);
                    }
                }
                swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onFailure(String errorCode, String message) {
                scrollListener.setLoadFinish(true);
                ToastUtil.showShort(getBaseContext(), message);
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestData();
    }
}
